package com.midea.ai.air.ui.user.fragment;

import android.app.Dialog;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.home.MainActivityNew;
import com.midea.ai.air.ui.user.LoginActivity;
import com.midea.ai.air.utils.StringUtil;

/**
 * Created by hww on 2016/3/22.
 */
public class RegisterFragment extends Fragment implements View.OnClickListener {

    private Button register_bt;
    private Button verification_code_bt;
    private Button verification_code_again_bt;
    private TextView register_error;
    private ImageView phone_img,password_img,vc_img;
    private EditText register_phone, register_password, verification_code;
    private LinearLayout next_linearlayout, register_linearlayout;
    private Dialog send_vd_Dialog, register_Dialog;


    private static final int REGISTER_SECCESS = 0X111;
    private static final int VERIFICATION_CODE_SECCESS = 0X110;


    Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case REGISTER_SECCESS:
                    if (send_vd_Dialog != null) {
                        send_vd_Dialog.dismiss();
                    }

                    next_linearlayout.setVisibility(View.GONE);
                    register_linearlayout.setVisibility(View.VISIBLE);
                    register_bt.setVisibility(View.GONE);
                    verification_code_bt.setVisibility(View.VISIBLE);
                    verification_code_again_bt.setVisibility(View.VISIBLE);
                    break;
                case VERIFICATION_CODE_SECCESS:
                    if (register_Dialog != null) {
                        register_Dialog.dismiss();
                    }

                    startActivity(new Intent(getActivity(), MainActivityNew.class));
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_register, container, false);

        initView(view);

        return view;
    }


    private void initView(View view) {

        register_bt = (Button) view.findViewById(R.id.register_bt);
        verification_code_bt = (Button) view.findViewById(R.id.register_bt_verification_code);
        verification_code_again_bt = (Button) view.findViewById(R.id.register_bt_verification_code_again);
        register_error = (TextView) view.findViewById(R.id.register_top_error);
        register_phone = (EditText) view.findViewById(R.id.register_phone);
        register_password = (EditText) view.findViewById(R.id.register_password);
        verification_code = (EditText) view.findViewById(R.id.register_verification_code);
        next_linearlayout = (LinearLayout) view.findViewById(R.id.register_linearLayout_phone_password);
        register_linearlayout = (LinearLayout) view.findViewById(R.id.register_linearLayout_verification_code);
        phone_img = (ImageView)view.findViewById(R.id.register_imageView_phone);
        password_img= (ImageView)view.findViewById(R.id.register_imageView_password);
        vc_img= (ImageView)view.findViewById(R.id.register_imageView_verification_code);

        register_bt.setOnClickListener(this);
        verification_code_bt.setOnClickListener(this);
        verification_code_again_bt.setOnClickListener(this);
        verification_code_again_bt.setBackgroundResource(R.color.register_bg);
        register_bt.setAlpha(0.25f);

        send_vd_Dialog = LoginActivity.createLoadingDialog(getActivity(), R.string.dialog_send_verification_code);
        register_Dialog = LoginActivity.createLoadingDialog(getActivity(), R.string.dialog_register);


        //下一步按钮激活
        register_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(register_phone.getText())) {
                    if (count < 0) {
                        register_bt.setAlpha(0.25f);
                    } else {
                        register_bt.setAlpha(1f);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        //注册按钮激活
        verification_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(register_phone.getText())) {
                    if (count < 0) {
                        verification_code_bt.setAlpha(0.25f);
                    } else {
                        verification_code_bt.setAlpha(1f);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        //输入框焦点，判断是否显示图标
        register_phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    phone_img.setVisibility(View.VISIBLE);
                } else {
                    phone_img.setVisibility(View.INVISIBLE);
                }
            }
        });

        register_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    password_img.setVisibility(View.VISIBLE);
                } else {
                    password_img.setVisibility(View.INVISIBLE);
                }
            }
        });

        verification_code.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    vc_img.setVisibility(View.VISIBLE);
                } else {
                    vc_img.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.register_bt:
                //注册下一步按钮
                register();
                break;
            case R.id.register_bt_verification_code:
                //注册按钮
                verification_register();
                break;
            case R.id.register_bt_verification_code_again:
                //重发验证码按钮

                break;

            default:
                break;
        }

    }


    //注册的方法
    private void register() {
        next_linearlayout.setVisibility(View.VISIBLE);
        register_linearlayout.setVisibility(View.GONE);
        register_bt.setVisibility(View.VISIBLE);
        verification_code_bt.setVisibility(View.GONE);
        verification_code_again_bt.setVisibility(View.GONE);
        String phone = register_phone.getText().toString().trim();
        String password = register_password.getText().toString().trim();


        if (TextUtils.isEmpty(phone)) {
            register_error.setText(R.string.log_in_phone_empty);
            return;
        }
        if (TextUtils.isEmpty(password)) {
            register_error.setText(R.string.log_in_password_empty);
            return;
        }

        if (phone.length() != 11) {
            register_error.setText(R.string.log_in_phone_eleven);
            return;
        }

        if (!StringUtil.isMobileNO(phone)) {
            register_error.setText(R.string.log_in_phone_error);
            return;
        }

        if (password.length() < 6 || password.length() > 16) {
            register_error.setText(R.string.register_password_error);
            return;
        }

        if (LoginActivity.isSpecialCharacter(password)) {
            register_error.setText(R.string.register_password_error);
            return;
        }
        register_error.setText("");

        send_vd_Dialog.show();
        handler.sendEmptyMessageDelayed(REGISTER_SECCESS, 1800);
    }


    /**
     * 填写提交验证码注册
     */
    private void verification_register() {
        String phone = verification_code.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            LoginActivity.myToast(getActivity(), R.string.top_verification_code_empty);
            return;
        }

        register_Dialog.show();
        handler.sendEmptyMessageDelayed(VERIFICATION_CODE_SECCESS, 1800);

    }

}